package hanjia;

import java.util.function.Consumer;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2025/1/19-下午12:34
 * Since  :   1.0
 * Version:   1.0
 */
public class SinglyLinkedList {
    private Node head=new Node(111,null);

    private static class Node {
        int value;
        Node next;

        public Node(int value, Node next) {
            this.value = value;
            this.next = next;
        }
    }

    private Node findLast() {


        Node temp = head.next;
        if (temp==null){
            return head;
        }

        while (temp.next != null) {

            temp = temp.next;
        }
        return temp;


    }

    private Node findNode(int index) {
        if (index < 0) {
            return null; // 索引无效，返回null
        }
        int curIndex = 0;
        Node temp = head.next;
        while (temp != null) {
            if (curIndex == index) {
                break;
            }
            temp = temp.next;
            curIndex++;
        }
        return temp;

    }

    public void addFirst(int value) {

        head.next = new Node(value, head.next);

    }

    public void loop(Consumer<Integer> consumer) {
        Node temp = head.next;
        while (temp != null) {

            consumer.accept(temp.value);
            temp = temp.next;
        }

    }

    public void addLast(int value) {
        Node last = findLast();
        last.next = new Node(value, null);
    }



    public int getValue(int index) {
        Node node = findNode(index);
        if (node == null) {
            throw new RuntimeException();
        }
        return node.value;
    }

    public void addByIndex(int index, int element) {

        if (index < 0) {
            throw new IndexOutOfBoundsException("索引不能为负数"); // 检查索引有效性
        }
        if (index == 0) {
            addFirst(element);
            return;
        }

        Node node = findNode(index - 1);

        if (node == null) {
            throw new IndexOutOfBoundsException("索引超出链表范围"); // 检查索引是否超出范围
        }

        Node newNode = new Node(element, node.next);
        node.next = newNode;


    }

    public void removeFirst(){

        if (head.next==null){
            return;
        }

        head.next=head.next.next;
    }

    public void removeByIndex(int index){
        if (index<0){
            throw new IndexOutOfBoundsException("索引不能为负数"); // 检查索引有效性
        }
        if (index==0){
            removeFirst();
            return;
        }

        Node pre = findNode(index - 1);
        if (pre==null){
            throw new RuntimeException();
        }

        Node now = findNode(index );
        if (now==null){
            throw new RuntimeException();
        }


        pre.next=now.next;


    }

    public static void main(String[] args) {
        SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
        singlyLinkedList.addLast(1);
        singlyLinkedList.addLast(2);
        singlyLinkedList.addLast(3);
        singlyLinkedList.addLast(4);

        singlyLinkedList.loop(value->{
            System.out.println(value);
        });

    }


}

